var app = getApp();
var maxPageIndex;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    ifShownoData: true,
    wrapCategory: false,
    orderStatus: [
      //状态:1进站 2定价 3待审核 4未支付 5已结算 6 取消
      {
        statusName: '全部',
        statusCode: ''
      },
      {
        statusName: '待支付',
        statusCode: '4'
      },
      {
        statusName: '已支付',
        statusCode: '5'
      },
      {
        statusName: '已取消',
        statusCode: '6'
      }
    ],
    statusSelect: '',
    listDate: [
      {
        name: '全部',
        id: 1
      },
      {
        name: '近7天',
        id: 2
      },
      {
        name: '近一个月',
        id: 3
      },
      {
        name: '近三个月',
        id: 4
      },
      {
        name: '3个月前',
        id: 5
      }
    ],
    dateSelect: 1,
    listCategory: [
      {
        name: '全部',
        id: ''
      },
      {
        name: '待定价',
        id: 2
      },
      {
        name: '待审核',
        id: '3'
      },
      {
        name: '待支付',
        id: 4
      },
      {
        name: '已支付',
        id: 5
      },
      {
        name: '已取消',
        id: 6
      },
      {
        name: '待回皮',
        id: 7
      }
    ],
    categorySelect: '',
    buthiddenCancel: false,
    pageIndex: 1,
    result: []
  },
  onShow() {
    wx.pageScrollTo({ scrollTop: 0, duration: 0 });
    this.setData({
      pageIndex: 1
    });
    this.getData();
  },
  // findOrderUser
  getData: function() {
    var that = this;
    wx.showLoading({ title: '加载中' });
    wx.request({
      method: 'POST',
      url: app.preUrl + 'findOrderUser',
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        userId: app.globalData.userId,
        page: this.data.pageIndex, //当前页
        size: 8, //条数
        orderStatus: that.data.statusSelect,
        timeSection: this.data.dateSelect
      },
      success: function(res) {
        wx.hideLoading();
        console.log(res);
        if (res.data.code == 100) {
          let data = res.data.result;
          console.log(data);
          maxPageIndex = data.total / 8;

          if (that.data.pageIndex != 1) {
            var array = that.data.result;
            array = array.concat(data.result);
            that.setData({ result: array });
          } else {
            that.setData({ result: data.result });
          }
          that.setData({ ifShownoData: true });
        } else if (res.data.code == 400) {
          that.setData({ ifShownoData: false, result: [] });
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.message,
            confirmColor: '#56BDE0',
            showCancel: false
          });
        }
      },
      fail: function(error) {}
    });
  },
  //取消订单事件
  ifCancel: function(e) {
    var that = this;
    var id = e.currentTarget.dataset.id;

    wx.showModal({
      // title: '提示',
      content: '确定取消该订单吗？',
      cancelText: '再等等',
      confirmColor: '#50BEC5',
      success: function(res) {
        if (res.confirm) {
          that.setData({ buthiddenCancel: true });
          wx.request({
            method: 'POST',
            url: app.preUrl + 'updateOrderStatus',
            dataType: 'json',
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            data: {
              id: id,
              orderStatus: 6
            },
            success: function(res) {
              if (res.data.code === 100) {
                that.setData({
                  buthiddenCancel: false,
                  pageIndex: 1
                });
                that.getData();
              } else {
                wx.showModal({
                  title: '提示',
                  content: res.data.message,
                  confirmColor: '#56BDE0',
                  showCancel: false
                });
                that.setData({ buthiddenCancel: false });
              }
            },
            fail: function(error) {
              that.setData({ buthiddenCancel: false });
            }
          });
        } else if (res.cancel) {
          // console.log('用户点击取消')
        }
      }
    });
  },
  //去定价
  toPrice: function(e) {
    let data = e.currentTarget.dataset.order;
    wx.navigateTo({ url: 'priced?data=' + JSON.stringify(data) });
  },
  //去审核
  toExamine(e) {
    let id = e.currentTarget.dataset.id;
    let data = e.currentTarget.dataset.order;
    wx.navigateTo({
      url: 'order_checked?id=' + id + '&data=' + JSON.stringify(data)
    });
  },
  //去付款
  toPay: function(e) {
    let data = e.currentTarget.dataset.order;
    wx.navigateTo({
      url: 'payment?data=' + JSON.stringify(data)
    });
  },
  //查看图片
  toOrderPicture: function(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: 'order_picture?id=' + id
    });
  },
  //一级菜单选择
  orderStatusList: function(e) {
    var that = this;
    console.log(e.currentTarget.dataset.select);
    that.setData({
      //把选中值放入判断值
      statusSelect: e.currentTarget.dataset.select
    });
    wx.pageScrollTo({ scrollTop: 0, duration: 0 });
    this.setData({
      pageIndex: 1
    });
    this.getData();
  },
  //月份选择
  dateList: function(e) {
    var that = this;
    that.setData({
      //把选中值放入判断值
      dateSelect: e.currentTarget.dataset.select
    });
  },
  //二级菜单选择
  categoryList: function(e) {
    var that = this;
    that.setData({
      //把选中值放入判断值
      statusSelect: e.currentTarget.dataset.select,
      categorySelect: e.currentTarget.dataset.select
    });
  },
  showModal: function() {
    var that = this;
    that.showModal();
  },
  onSure: function() {
    var that = this;
    that.hideModal();
    wx.pageScrollTo({ scrollTop: 0, duration: 0 });
    this.setData({
      pageIndex: 1
    });
    this.getData();
  },
  //显示弹窗事件
  showModal: function(e) {
    var that = this;
    // 创建一个动画实例
    var animation = wx.createAnimation({
      // 动画持续时间
      duration: 300,
      // 定义动画效果，当前是匀速
      timingFunction: 'linear'
    });
    // 将该变量赋值给当前动画
    that.animation = animation;
    // 先在y轴偏移，然后用step()完成一个动画
    animation.translateY(-200).step();
    // 用setData改变当前动画
    that.setData({
      // 通过export()方法导出数据
      animationData: animation.export(),
      // 改变view里面的Wx：if
      //wrapCategory: true,
      wrapCategory: true
    });
    // 设置setTimeout来改变y轴偏移量，实现有感觉的滑动
    setTimeout(function() {
      animation.translateY(0).step();
      that.setData({
        animationData: animation.export()
      });
    }, 0);
  },
  //隐藏弹窗事件
  hideModal: function(e) {
    var that = this;
    var animation = wx.createAnimation({
      duration: 1000,
      timingFunction: 'linear'
    });
    that.animation = animation;
    animation.translateY(-200).step();
    that.setData({
      animationData: animation.export()
    });
    setTimeout(function() {
      animation.translateY(0).step();
      that.setData({
        wrapCategory: false,
        animationData: animation.export()
      });
    }, 100);
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;
    if (maxPageIndex <= that.data.pageIndex) {
      console.log('到底了');
      return;
    }
    that.setData({
      pageIndex: that.data.pageIndex + 1
    });

    // 上拉获取更多数据
    that.getData();
  },
  onPullDownRefresh: function() {
    var that = this;
    that.setData({ pageIndex: 1, result: [] });
    this.getData();
    wx.stopPullDownRefresh();
  }
});
